package  {
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import ImageSplitter;
	import SplitEvent;
	
	public class SuccesfulSplit extends MovieClip {
		
		private var _splitter:ImageSplitter;
		private var _imageParts:Array;
		private var _stageHeight:Number;
		
		public function SuccesfulSplit() {
			_splitter = new ImageSplitter("banners/banner1.jpg");
			//_imageParts = _splitter.imageParts;
			//trace(_imageParts.length);
			_splitter.addEventListener(SplitEvent.SPLIT_COMPLETE, onSplit);
		}
		
		private function onSplit(e:SplitEvent):void {
			
			//file _imageParts with image pieces form ImageSplitter
			_imageParts = _splitter.imageParts;
			
			//set stage height
			_stageHeight = stage.stageHeight;
			//loop through images and add them to a sprite then add the sprites to the stage. 
			for (var i = 0; i < _imageParts.length; i++) {
				var sprite:Sprite = new Sprite();
				sprite.name = "imgSprite" + i;
				sprite.addChild(_imageParts[i]);
				addChild(sprite);
				var tempSprite:DisplayObject = getChildByName("imgSprite" + i);
				
				//center vertically
				tempSprite.y = (_stageHeight / 2) - (tempSprite.height / 2);
				
				//space out horizontally with 5px gap
				tempSprite.x = i * (tempSprite.width + 5);
			}
		}
	}	
}